package com.zhaopin.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Compare4 {

    public static String bigTree_exp = "config-resume_recommend-resume_talents-SALE-v1.5.1-publicStore-Feature";

    //下边三行结果，需要改一下
    public static String publicStore_exp = "config-resume_recommend-resume_talents-SALE-v1.5.1-publicStore";
    public static String rulePath = "/ROOT/Translatetranslaterv10";

    public static String jobNumbers ="CC720939240J40110713111 CCL1271415410J40107414407 CC885797390J40067263905 CC120012142J00306113110 CC155825710J40100025001 CC747631280J40108011715 CC896090830J00382391904 CCL1288562110J40121667702 CCL1322682100J40122829910 CC269979810J40111722911 CC403371410J40114524011 CZ726586090J00007308602 CC564035030J40107026903 CCL1211134540J40028784414 CC472074580J40121511510 CC259560680J40107030508 CC676872820J40118833708 CC427023280J40124915015 CC602881481J00123091513 CC600839820J40077190207 CCL1289017370J40054117309 CC312656380J40034319016 CC512756820J00290326003 CCL1283652660J40108516204 CCL1283463240J40035205107 CCL1254037750J40097212305 CC299828080J00233614311 CC598862420J40113518914 CC881828890J40079668304 CC868628270J40073167204 CC172509610J40053361113 CCL1230797890J40099238313 CC835637840J40076161905 CC251073030J40116601301 CCL1230953470J40119486509 CCL1200184710J40109096308 CCL1242657980J00523115707 CC477294382J00495024707 CC855716740J40117571904 CCL1318004950J40106167304 CCL1283713190J40090582201 CC406905530J40098419009 CCL1317888620J40104904816 CC324508510J40065643412 CC608011980J40115458111 CC857262570J40130079514 CC552647239J00071397412 CCL1200883850J40039825503 CCL1298545830J40093420701 CC395547030J40112336411 CC748427540J40060118011 CC490532180J00310329403 CC216145320J40109180101 CCL1295292360J40091307812 CC307702430J40101814406 CC897452820J40102082612 CCL1298644430J40080877904 CC636205020J40114892113 CC721797340J00561147507 CCL1309992630J40100554108 CCL1262773630J40036124605 CC281642380J40116951110 CC697590330J40098536206 CCL1288245330J40114929205 CCL1266467250J40118012903 CCL1293585700J40115712901 CCL1296810920J40078850110 CCL1315261270J40116082813 CC120534548J00293456007 CC847176470J00279401809 CC155825710J40111217701 CC158991610J40123020615 CC427023280J40122329815 CZ136374120J00374976805 CC503718880J40116695906 CC564349280J40116410013 CC538179080J40119112603 CC874115250J40026309103 CC120048960J40118214416 CCL1286699510J40041531511 CC202282121J00414606205 CC252944430J00236686015 CC566314388J00028098312 CC872644240J40094090804 CCL1286791240J40111181307 CC829017560J40054686709 CC892195490J00469809906 CC358986320J40122672910 CC287437110J40038478413 CCL1242876790J40116864812 CCL1209798740J40112956906 CC624832626J00516621607 CCL1248031850J40119553115 CC155825712J00251482713 CC820453880J40110581605 CC462839380J40109182607 CCL1296232540J40125267508 CC524929029J00069815309 CC255946310J40123882210 CC846689600J40115555601 CC120048960J40118218316 CCL1322328830J40125493408 CC374163030J40097564903 CC870130550J40086011102 CC306183130J40125695707 CC884385720J40115266201 CC190360320J40059822408 CC580117130J40093413605 CCL1277522930J40080479802 CC857067010J40115497211 CZ273859680J00195555501 CC000129280J40092755301 CC868345580J40107016209 CC155825710J40092712901 CC333560916J00217710415 CC832999060J00511619002 CC594346120J40120475610 CC141369160J40114428609 CC307313210J40099127801 CC160867210J40108683103 CC441918188J00042403804 CZ623323980J00023044811 CC692483530J00458226102 CCL1250861530J40099467313 CCL1242515720J40078690103 CCL1312788210J40106836704 CC284285180J40107814813 CC312759633J90250186000 CC872648410J40111052508 CC542388920J40074874004 CC318200380J40059187608 CCL1262855360J40073409507 CC318200380J40118794208 CC713588980J40082437004 CCL1242430090J40128600814 CC155825712J00292354713 CC626038520J40096656406 CC577861030J40109254805 CC852678290J00356937206 CC520034920J40113980802 CC317978430J40034450010 CCL1242515720J00678455805 CC856001370J00163176609 CC138944016J00238918212 CC308548286J00671458905 CC613457080J40112645701 CC460580180J40115177704 CCL1242515720J00591476305 CCL1258899510J40114590303 CC821508450J00413271406 CC538540980J40108459312 CCL1319183360J40114824215 CC870732400J40102666412 CCL1285263550J40119761615 CC444000630J40119629416 CCL1319255710J40124721407 CCL1272202000J40122189910 CC424628480J40115810804 CC846735880J40111498815 CCL1301507380J40119437803 CZ449260480J00127808411 CC365889810J40115178301 CC427023280J40123620615 CC362517510J40106172005 CC459084580J40098877012 CC827112000J40105351608 CC200697280J40056420509 CC403371418J00441911606 CC552328620J40015309508 CC318200385J00547955007 CC586369880J40046233501 CC840191560J40030412515 CC877528180J40117178508 CC245959280J40111807607 CC337202612J90253702000 CC596285130J40101816313 CC333757330J40059614413 CC217000521J00211267112 CCL1290098510J40115333502 CC271354230J40084221010 CC318200380J40125753908 CC821926340J40079356206 CCL1238006870J40074839706 CCL1321427500J40118410903 CCL1312038980J40109278806 CCL1256135840J40073895007 CZ846199170J00115155713 CC657223320J40003335907 CCL1257273220J40059245909 CC725567440J40091977015";
    public static String compareResultFile = "e:\\compare.json";
    public static void main(String[] args) throws IOException {

        String url = "https://zpsearch.zhaopin.com/aggregatorpre/resume-recommend/resume/talents";
        String onlineUrl = "https://zpsearch.zhaopin.com/aggregator/resume-recommend/resume/talents";
        String[] jns = jobNumbers.split(" ");

        JSONObject resultJson = new JSONObject();
        for(int i = 0 ;i < jns.length;i++){
            JSONObject jsonObj = new JSONObject();
            JSONObject solr = new JSONObject();
            JSONArray fq = new JSONArray();
            jsonObj.put("solrExp" ,solr);
            jsonObj.put("fq",fq);
            String jobNumber = jns[i];
            resultJson.put(jobNumber,jsonObj);
            String publicStoreParam = getParam(publicStore_exp, jobNumber,null);
            String bigTreeParam = getParam(bigTree_exp, jobNumber,rulePath);



            JSONObject publicStoreResult = null;
            JSONObject publicStoreRules = null;
            boolean retryFlag = true;
            while(retryFlag) {
                try {
                    publicStoreResult = httpPost(url, publicStoreParam);
                    publicStoreRules = getRuneRank(publicStoreResult);
                    retryFlag = false;
                } catch (Exception e) {
                    System.out.println("开始重试publicStore" + jobNumber);
                }
            }
            JSONObject bigTreeResult = null;
            JSONObject bigTreeRules = null;
            retryFlag = true;
            while(retryFlag) {
                try {

                    bigTreeResult = httpPost(url, bigTreeParam);
                    bigTreeRules = getRuneRank(bigTreeResult);
                    retryFlag = false;

                } catch (Exception e) {
                    System.out.println("开始重试BigTree" + jobNumber);
                }
            }


            System.out.println(jobNumber + "   rule size same:" + (publicStoreRules.size() == bigTreeRules.size()));
            for(Map.Entry<String,Object> entry:publicStoreRules.entrySet()){
                String publicStoreSolr = "";
                String bigTreeSolr = "";
                String ruleName = entry.getKey();
                JSONObject valueJson = (JSONObject) entry.getValue();
                JSONObject contextJson = valueJson.getJSONObject("context");
                if(contextJson != null){
                    publicStoreSolr = contextJson.getString("context");
                }

                valueJson = null;
                for(Map.Entry<String,Object> ee : bigTreeRules.entrySet()){
                    String rn = ee.getKey();
                    if(rn.contains(ruleName)){
                        valueJson = (JSONObject)ee.getValue();
                    }
                }
//                valueJson = bigTreeRules.getJSONObject(ruleName+"_fex");



                contextJson = valueJson.getJSONObject("context");
                if(contextJson != null){
                    bigTreeSolr = contextJson.getString("context");
                }
                if(!bigTreeSolr.equals(publicStoreSolr)){
                    JSONObject ruleJsonObj = new JSONObject();
                    ruleJsonObj.put("publicS",publicStoreSolr);
                    ruleJsonObj.put("bigTree",bigTreeSolr);
                    solr.put(ruleName,ruleJsonObj);
                }
            }

            //fq
            JSONArray pubFq = publicStoreResult.getJSONObject("debugs").getJSONObject("invokeInfo").getJSONArray("solr").getJSONObject(0).getJSONObject("request").getJSONObject("param").getJSONArray("fq");
            JSONArray bigtreeFq =   bigTreeResult.getJSONObject("debugs").getJSONObject("invokeInfo").getJSONArray("solr").getJSONObject(0).getJSONObject("request").getJSONObject("param").getJSONArray("fq");
            for(int index = 0 ;index < pubFq.size();index++){
                String pubFqExp = pubFq.getString(index);
                String bigtreeFqExp = bigtreeFq.getString(index);
                if(!pubFqExp.equals(bigtreeFqExp)){
                    JSONObject fqcompare = new JSONObject();
                    fqcompare.put("bigTree",bigtreeFqExp);
                    fqcompare.put("publicS",pubFqExp);
                    fq.add(fqcompare);
                }
            }
        }
        FileOutputStream fos = new FileOutputStream(new File(compareResultFile));
        fos.write(JSON.toJSONString(resultJson, SerializerFeature.DisableCircularReferenceDetect).getBytes(Charset.forName("UTF-8")));
        fos.close();

    }

    private static JSONObject getRuneRank(JSONObject resultJson){
        JSONObject debugsJson = resultJson.getJSONObject("debugs");
        JSONObject ruleRankNode = debugsJson.getJSONObject("ruleRank");
        JSONObject rr0Json = ruleRankNode.getJSONObject("0");
        JSONObject ruleRank = rr0Json.getJSONObject("rankRule");
        return ruleRank;
    }




    private static String getParam(String exp, String jobNumber,String rulePath) {
        JSONObject jo = new JSONObject();
        //debug
        JSONObject debug = new JSONObject();
        debug.put("simple", true);
        debug.put("exp", exp);
//        debug.put("function", true);
        jo.put("debug", debug);
        //debugs
        JSONObject debugs = new JSONObject();
        JSONObject system = new JSONObject();
        system.put("rule", true);
        if(rulePath != null){
//            system.put("rulePath",rulePath);
        }
        debugs.put("system", system);
        jo.put("debugs", debugs);
        //============
        jo.put("aggregator_protocol", "rpc");
        jo.put("CompanyId", "39493793");
        jo.put("aggregator_core_version_resume_search_resume_v3", "1.1");
        jo.put("start", "0");
        jo.put("rows", "0");
        jo.put("S_DISCLOSURE_LEVEL", "3:2");
        jo.put("eventScenario", "talents");
        jo.put("UserId", "1049895014");
        jo.put("client", "homepage");
        jo.put("DepartmentId", "39493793");
        jo.put("jobNumber", jobNumber);
        jo.put("rankStype_1", true);

        JSONObject phase = new JSONObject();
        JSONObject experiment = new JSONObject();
        JSONObject custom = new JSONObject();
        custom.put("timeout",5000000);

        experiment.put("custom",custom);
        phase.put("experiment", experiment);
        debugs.put("phase", phase);
        return jo.toJSONString();
    }
    private static JSONObject httpPost(String url, String param) {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        HttpPost httpPost = new HttpPost(url);

        StringEntity entity = new StringEntity(param, "UTF-8");

        httpPost.setEntity(entity);
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();

            if (responseEntity != null) {
                String content = EntityUtils.toString(responseEntity);
                return JSONObject.parseObject(content);
            }
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
